Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: "0",//结果处显示的数字
    num1: 0,//操作数1
    num2: 0,//操作数2
    operator: ""//操作符
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  clickButton: function (e) {
    var id = e.currentTarget.id;
    if (id >= 'num_0' && id <= 'num_9') {//数字键
      var result = this.data.result;
      result += id.split("_")[1];
      if (this.data.result == '0') {
        result = id.split("_")[1];
      }
      this.setData({
        result: result,
        num1: parseFloat(result)
      })
    } else {//功能键;
      var num1 = this.data.num1;
      var operator = this.data.operator;
      switch (id) {
        case "clear":
          this.setData({
            result: "0",
            num1: 0,
            operator: ""//操作符
          });
          break;
        case "back":
          var result = this.data.result;
          if (result.length > 1) {
            result = result.substring(0, result.length - 1);
          } else {
            result = 0;
          }
          this.setData({
            result,
            num1: parseFloat(result)
          })
          break;
        case "dot":
          var result = this.data.result;
          if (result.indexOf(".") == -1) {
            result += ".";
            this.setData({
              result,
              num1: parseFloat(result)
            })
          }
          break;
        // case "negative":
        //   data = -1 * data;
        //   break;
        // case "div":
        //   data = this.calculate(tep, operator, data);
        //   tmp = data;
        //   lastoper1 = "/";
        //   break;
        // case "mul":
        //   data = this.calculate(tep, operator, data);
        //   tmp = data;
        //   lastoper1 = "/";
        //   break;
        case "add":
          result = this.calculate(num1, operator, result);
          num1 = result;
          lastoper1 = "+";
          break;
        // case "sub":
        //   data = this.calculate(tep, operator, data);
        //   tmp = data;
        //   lastoper1 = "/";
        //   break;
        case "equ":
          data = this.calculate(tep, operator, data);
          tmp = data;
          lastoper1 = "/";
          break;
      }
    }
  },

  calculate: function (data1, oper, data2) {
    var data;
    data1 = parseFloat(data1);
    data2 = parseFloat(data2);
    switch (oper) {
      case "+":
        data = data1 + data2;
        break;
      case "-":
        data = data1 - data2;
        break;
      case "*":
        data = data1 * data2;
        break;
      case "/":
        if (data2 !== 0) {
          data = data1 / data2;
        } else {
          data = 0;
        }
        break;
    }
    return data;
  }
})